package com.phaeton.beemeup.entity;

import java.util.UUID;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.phaeton.beemeup.GameManager;

public class BeeComb extends Comb implements IComb{
	protected int honey = 0;
	protected boolean selected = false;
	private TimerHandler damageHandler;
	
	public BeeComb(int col, int row, ITiledTextureRegion pTiledTextureRegion, GameManager pGameManager) {
		super(col, row, pTiledTextureRegion, pGameManager);
	}

	public int getHoney() {
		return super.getResource();
	}

	public void changeHoney(int i) {
		super.changeResource(i);
	}

	public void setHoney(int honey) {
		super.setResource(honey);
	}
	
	public void changeStructure(int structure) {
		super.changeStructure(structure);
	}
	
	public int getStructure() {
		return super.getStructure();
	}
	@Override
	public void damageComb(final UUID combID, final Insect attacker) {
		//auf 5 aendern
		Log.e("BeeComb", "damage started");
		this.registerUpdateHandler(new TimerHandler(2, true, new ITimerCallback() {
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				Log.e("BeeComb", "Structure: " + structure + " und Honey: " + honey);
				if(honey > 0) {
					changeHoney(-1);
				}
				else {
					if(getStructure() > 0) {
						//damageHandler.setTimerSeconds(10);
						changeStructure(-1);
					}
					else {
						BeeComb.this.unregisterUpdateHandler(pTimerHandler);
						attacker.removeState(Insect.FIGHT);
						mGameManager.destroyBeeComb(combID);
						//pTimerHandler.reset();
					}
				}
			}			
		}));
	}
		
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if(pSceneTouchEvent.isActionDown() && (this.getState() & (IComb.OCCUPIED | IComb.FIGHT)) == 0){
			if(mGameManager.getSelectedBee() != null){
				Bee bee = mGameManager.getBees().get(mGameManager.getSelectedBee());
				bee.setTarget(this);
				bee.fly();
				return true;
			}
			
			if(this.getHoney() >= 5){
				if(!selected){
					selected = true;
					
					if(row > 0 && mGameManager.getLevel().getCell(col, row - 1).getState() == Cell.FREE)
						mGameManager.showNewComb(this, col, row-1);
					
					if(row < mGameManager.getLevel().getDimension().getRow() - 1 && mGameManager.getLevel().getCell(col, row + 1).getState() == Cell.FREE) 
						mGameManager.showNewComb(this, col, row+1);
					
					if(col > 0 && mGameManager.getLevel().getCell(col-1, row).getState() == Cell.FREE) 
						mGameManager.showNewComb(this, col-1, row);
					
					if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && mGameManager.getLevel().getCell(col+1, row).getState() == Cell.FREE) 
						mGameManager.showNewComb(this, col+1, row);
					
					if(col % 2 == 0){
						if(col > 0 && row > 0 && mGameManager.getLevel().getCell(col - 1, row - 1).getState() == Cell.FREE)
							mGameManager.showNewComb(this, col-1, row-1);
						
						if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && row > 0 && mGameManager.getLevel().getCell(col+1, row - 1).getState() == Cell.FREE) 
							mGameManager.showNewComb(this, col+1, row-1);
					}else{
						if(col > 0 && row < mGameManager.getLevel().getDimension().getRow() - 1 && mGameManager.getLevel().getCell(col - 1, row + 1).getState() == Cell.FREE)
							mGameManager.showNewComb(this, col-1, row+1);
						
						if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && row < mGameManager.getLevel().getDimension().getRow() - 1 && mGameManager.getLevel().getCell(col+1, row + 1).getState() == Cell.FREE) 
							mGameManager.showNewComb(this, col+1, row+1);
					}
				}else{
					selected = false;
					
					mGameManager.hideNewCombs();
				}
				return true;
			}
		}
		return false;
	}
}
